package com.music.action.detail;

import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.music.action.BaseAction;
import com.music.action.BaseKey;
import com.music.common.ParamUtil;
import com.music.detail.service.VedioDetailService;
import com.music.domain.Commit;
import com.music.domain.Video;
import com.music.domain.VideoCommit;

/**
 * 视频Detail相关
 * @author LFSenior
 *
 */
public class VedioDetailAction  extends BaseAction{
	
	@Autowired
	VedioDetailService vedioDetailService;
	/**
	 * 跳转到视频详情页面
	 * @return
	 */
	@Action(value="loadVideoDetail",results={@Result(name="vediodetail",location="video-detail.jsp")})
	public String redicterVedioDetail() throws Exception{
		Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
		request.put(BaseKey.VIDEO_DETAIL, findVideoById(param.get("videoId").toString()));
		return "vediodetail";
	}
	
	/**
	 * 根据视频ID查询相关视频信息
	 * @param videoId
	 * @return
	 */
	public Video findVideoById(String videoId){
		return vedioDetailService.findVideoById(videoId);
	}
	
	/**
	 * 加载commit相关内容
	 * @return
	 * @throws Exception
	 */
	@Action(value="commitvidoe",results={@Result(name="commitvidoe",location="track-detail/commitvideo.jsp")})
	public String redicterCommit() throws Exception{
		Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
		/**
		 * 查找评论的类容
		 */
		request.put(BaseKey.VIDEO_COMMIT,findCommitsById(param.get("videoid").toString()));
		return "commitvidoe";
	}
	
	
	/**
	 * 评论
	 * @return
	 */
	@Action(value="submitCommitVideo",results={@Result(name=POST,type="json",params={"root","reParam"})})
	public String submitCommit(){
		try {
			Map<String, Object> param = ParamUtil.buildRequest(ServletActionContext.getRequest());
			if(param.get("createname")==null||param.get("createname").toString().trim()==""){
				param.put("createname", ParamUtil.buildUserName(ServletActionContext.getRequest()));
			}
			param.put("id", ParamUtil.buildUUID());
			vedioDetailService.addCommit(param);
			reParam.put("result", "T");
		} catch (Exception e) {
			reParam.put("result", "F");
		}
		return POST;
	}
	
	
	public List<VideoCommit> findCommitsById(String videoid){
		return vedioDetailService.findCommitsById(videoid);
	}
	
}
